﻿using System;
using System.Text;
using System.Web.UI;
using Be;
using System.Web.Security;
using DayPilot.Utils;
using DayPilot.Web.Ui.Enums;
using DayPilot.Web.Ui.Events;
using DayPilot.Web.Ui.Events.Bubble;
using CommandEventArgs = DayPilot.Web.Ui.Events.CommandEventArgs;
using WebGui.BllLessonService;
using System.Linq;
using DayPilot.Web.Ui.Events.Navigator;
using System.Collections.Generic;

namespace WebGui.Admin {

    /// <summary>
    /// This class controlle the Times available for the student
    /// </summary>
    public partial class AddTimes : Page {

        /// <summary>
        /// The cellbackground
        /// </summary>
        private string _CellBackGround = "#D9D9D9";

        /// <summary>
        /// The lesson color
        /// </summary>
        private string _LessonsColor = "#808080";

        /// <summary>
        /// The LessonServiceClient
        /// </summary>
        private LessonServiceClient _lesson;

        /// <summary>
        /// This is used to when the page load, then it makes the BllLesson() and Loads the Listbox contents
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e) {
            _lesson = (LessonServiceClient) Session["Lesson"];
            try {
                if(!IsPostBack) {
                    CalLessons.StartDate = Week.FirstDayOfWeek();
                    LoadLessons();
                    DataBind();
                }
            } catch(Exception ex) {
                ShowMessage(ex.Message);
            }
        }

        /// <summary>
        /// Loads the lessons in a specific week
        /// </summary>
        private void LoadLessons() {
            try {
                List<BeLesson> calendarlessons = _lesson.GetEvents(CalLessons.StartDate, CalLessons.Days).ToList();
                List<BeLesson> instructorlesson = new List<BeLesson>();
                foreach(BeLesson lesson in calendarlessons) {
                    if(lesson.UserId == Membership.GetUser().ProviderUserKey.ToString()) {
                        instructorlesson.Add(lesson);
                    }
                }
                CalLessons.DataSource = instructorlesson;
                CalLessons.DataBind();
                instructorlesson.Clear();
                int navEnd = (int) (CalNavigator.VisibleEnd - CalNavigator.VisibleStart).TotalDays;
                List<BeLesson> navigatorlessons = _lesson.GetEvents(CalNavigator.VisibleStart, navEnd).ToList();
                foreach(BeLesson lesson in navigatorlessons) {
                    if(lesson.UserId == Membership.GetUser().ProviderUserKey.ToString()) {
                        instructorlesson.Add(lesson);
                    }
                }
                CalNavigator.DataSource = instructorlesson;
                CalNavigator.DataBind();
            } catch(Exception ex) {
                ShowMessage(ex.Message);
            }
        }

        /// <summary>
        /// Show a message in a javascript alert box
        /// </summary>
        /// <param name="message">The message to show</param>
        private void ShowMessage(string message) {
            Response.Write("<script type='text/javascript'>alert('" + message + "'); </script>");
        }

        /// <summary>
        /// Moves an event
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        protected void CalLessons_EventMove(object sender, EventMoveEventArgs e) {
            try {
                int id = Convert.ToInt32(e.Value);
                BeLesson belesson = _lesson.GetLesson(id);
                string comment = e.Text;
                BeLesson lesson = new BeLesson(belesson.Id, e.NewStart, e.NewEnd, Membership.GetUser().ProviderUserKey.ToString(), comment, belesson.TypeId);
                string str = _lesson.EditLesson(lesson);
                LoadLessons();
                DataBind();
                CalLessons.Update(str);
            } catch(Exception ex) { ShowMessage(ex.Message); }
        }

        /// <summary>
        /// The calender navigation clicks
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        protected void CalLessons_Command(object sender, CommandEventArgs e) {
            switch(e.Command) {
                case "previous":
                    CalLessons.StartDate = CalLessons.StartDate.AddDays(-7);
                    LoadLessons();
                    CalLessons.DataBind();
                    CalLessons.Update(CallBackUpdateType.Full);
                    break;
                case "navigate":
                    DateTime start = (DateTime) e.Data["start"];
                    CalLessons.StartDate = start;
                    LoadLessons();
                    CalLessons.DataBind();
                    CalLessons.Update(CallBackUpdateType.Full);
                    break;
                case "refresh":
                    LoadLessons();
                    CalLessons.DataBind();
                    CalLessons.Update();
                    break;
            }
        }

        /// <summary>
        /// Loads the lessons
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        protected void CalNavigator_VisibleRangeChanged(object sender, VisibleRangeChangedEventArgs e) {
            LoadLessons();
        }

        /// <summary>
        /// A menu click in the event menu
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        protected void CalLessons_EventMenuClick(object sender, EventMenuClickEventArgs e) {
            try {
                if(e.Command == "Delete") {
                    int id = Convert.ToInt32(e.Value);
                    BeLesson target = _lesson.GetLesson(id);
                    string str = null;
                    if(target != null) {
                        BeLesson lesson = new BeLesson(id);
                        str = _lesson.DeleteLesson(lesson);
                    }
                    ShowMessage(str);
                    LoadLessons();
                    CalLessons.DataBind();
                    CalLessons.Update();
                }
            } catch(Exception ex) { ShowMessage(ex.Message); }
        }
        
        /// <summary>
        /// The event resize handler
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        protected void CalLessons_EventResize(object sender, EventResizeEventArgs e) {
            try {
                int id = Convert.ToInt32(e.Value);
                BeLesson lesson = new BeLesson(id, e.NewStart, e.NewEnd, Membership.GetUser().ProviderUserKey.ToString(), e.Text, 1);
                string str = _lesson.EditLesson(lesson);
                LoadLessons();
                CalLessons.Update(str);
            } catch(Exception ex) {
                ShowMessage(ex.Message);
            }
        }

        /// <summary>
        /// The event delete handler
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        protected void CalLessons_EventDelete(object sender, EventDeleteEventArgs e) {
            try {
                int id = Convert.ToInt32(e.Value);
                BeLesson lesson = new BeLesson(id);
                string str = _lesson.DeleteLesson(lesson);
                LoadLessons();
                DataBind();
                CalLessons.Update(str);
            } catch(Exception ex) {
                ShowMessage(ex.Message);
            }
        }

        /// <summary>
        /// The event select handler
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        protected void CalLessons_EventSelect(object sender, DayPilotEventArgs e) {
            CalLessons.Update();
        }

        /// <summary>
        /// The event edit handler
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        protected void CalLessons_EventEdit(object sender, EventEditEventArgs e) {
            try {
                int id = Convert.ToInt32(e.Value);
                BeLesson lesson = new BeLesson(id, e.NewText);
                string str = _lesson.Editcomment(lesson);
                LoadLessons();
                CalLessons.DataBind();
                CalLessons.Update(str);
            } catch(Exception ex) { ShowMessage(ex.Message); }
        }

        /// <summary>
        /// Render the content
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        protected void DayPilotBubble_RenderContent(object sender, DayPilot.Web.Ui.Events.Bubble.RenderEventArgs e) {
            if(e is RenderEventBubbleEventArgs) {
                RenderEventBubbleEventArgs re = e as RenderEventBubbleEventArgs;
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("<b>{0}</b><br />", re.Text);
                sb.AppendFormat("Start: {0}<br />", re.Start);
                sb.AppendFormat("End: {0}<br />", re.End);
                sb.AppendFormat("<br />");
                sb.AppendFormat("<b>Double-click the event to enter inline edit mode.</b>");
                re.InnerHTML = sb.ToString();
            } else if(e is RenderTimeBubbleEventArgs) {
                RenderTimeBubbleEventArgs re = e as RenderTimeBubbleEventArgs;
                e.InnerHTML = "<b>Time header details</b><br />From:" + re.Start + "<br />To: " + re.End;
            } else if(e is RenderCellBubbleEventArgs) {
                RenderCellBubbleEventArgs re = e as RenderCellBubbleEventArgs;
                e.InnerHTML = "<b>Cell details</b><br />Column:" + re.ResourceId + "<br />From:" + re.Start + "<br />To: " + re.End;
            }
        }

        /// <summary>
        /// Before the event rendering
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        protected void CalBookings_BeforeEventRender(object sender, DayPilot.Web.Ui.Events.Calendar.BeforeEventRenderEventArgs e) {
            e.BackgroundColor = _LessonsColor;
            e.DurationBarColor = _LessonsColor;
        }

        /// <summary>
        /// Before the cell rendering
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        protected void CalBookings_BeforeCellRender(object sender, DayPilot.Web.Ui.Events.BeforeCellRenderEventArgs e) {
            e.BackgroundColor = _CellBackGround;
        }
    }
}